package com.king.juc.syncmode;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock交替输出案例
 * @author king
 * @date 2020/11/23 16:32
 */
public class AwaitSignal extends ReentrantLock {

    private int loopNumber;

    public AwaitSignal(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public void print(String str, Condition now, Condition next){
        for (int i = 0; i < loopNumber; i++) {
            lock();
            try {
                try {
                    now.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.print(str);
                next.signal();
            }finally {
                unlock();
            }
        }
    }
}
